package com.cy.lib.util;
/**
 * Created by CY on 2015/4/27.
 */

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * @author CY
 * @title
 * @date 2015/4/27
 * @qq 568305883
 * @email tmxdyf@163.com
 */
public class TimeUtils {

    public static final String defaultFormat = "yyyy-MM-dd";

    public static String formatCurrentTime() {
        SimpleDateFormat format = new SimpleDateFormat(defaultFormat, Locale.CHINA);
        return format.format(new Date(System.currentTimeMillis()));
    }

    /**
     * 字符串转日期
     *
     * @param dateStr
     * @param formatStr
     * @return
     */
    public static Date strToDate(String dateStr, String formatStr) {

        SimpleDateFormat format = new SimpleDateFormat(formatStr);
        try {
            return format.parse(dateStr);
        } catch (java.text.ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 当前日期加天数
     *
     * @param days
     * @return
     */
    public static long dateAdd(int days) {
        return System.currentTimeMillis() + days * 24 * 60 * 60 * 1000;
    }

    /**
     * 当前日期加天数
     *
     * @param days
     * @return
     */
    public static String dateAddReturnStr(int days) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_MONTH, days);

        Date date = calendar.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
        return sdf.format(date);
    }


    /**
     * 友好时间
     *
     * @param dateStr
     * @param formatStr
     * @return
     */
    public static String friendlyTime(String dateStr, String formatStr) {

        Date date = strToDate(dateStr, formatStr);
        return friendlyTime(date.getTime());
    }


    /**
     * 友好时间
     *
     * @param time
     * @return
     */
    public static String friendlyTime(long time) {
        // 需要转换的时间
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(time);
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        int minute = calendar.get(Calendar.MINUTE);

        int week = calendar.get(Calendar.DAY_OF_WEEK);

        // 当前时间
        Calendar nowCalendar = Calendar.getInstance();
        int nowYear = nowCalendar.get(Calendar.YEAR);
        int nowMonth = nowCalendar.get(Calendar.MONTH);
        int nowDay = nowCalendar.get(Calendar.DAY_OF_MONTH);
        int nowHour = nowCalendar.get(Calendar.HOUR_OF_DAY);

        // 计算
        String hourStr = hour + "";
        String minuteStr = minute + "";
        if (hour < 10) {
            hourStr = "0" + hour;
        }
        if (minute < 10) {
            minuteStr = "0" + minute;
        }

        String timeStr = hourStr + ":" + minuteStr;

        if (year == nowYear && month == nowMonth) {// 当月
            // 凌晨(1：00—5：00)
            // 早上(5：00—8：00)
            // 上午(8：00—11：00)
            // 中午(11：00—13：00)
            // 下午(13：00—17：00)
            // 晚上(17：00—19：00)
            // 半夜(19：00—20：00)
            // 深夜(20：00—24：00)
            if (day == nowDay) {// 当天

                if (0 <= hour && hour < 5) {
                    return "凌晨" + timeStr;
                } else if (5 <= hour && hour < 8) {
                    return "早上" + timeStr;
                } else if (8 <= hour && hour < 11) {
                    return "上午" + timeStr;
                } else if (11 <= hour && hour < 13) {
                    return "中午" + timeStr;
                } else if (13 <= hour && hour < 17) {
                    return "下午" + timeStr;
                } else if (17 <= hour && hour < 19) {
                    return "傍晚" + timeStr;
                } else if (19 <= hour) {
                    return "晚上" + timeStr;
                }

                // if (hour < 11) {
                // return "上午" + timeStr;
                // } else {
                // return "下午" + timeStr;
                // }
            } else {
                int count = nowDay - day;
                switch (count) {
                    case 1:
                        return "昨天" + timeStr;
                    case 2: {
                        return weekStr(week) + timeStr;
                    }
                }
            }
        }

        return year + "-" + (month + 1) + "-" + day + " " + timeStr;
    }

    public static String weekStr(int week) {
        switch (week) {
            case Calendar.SUNDAY:
                return "周日";
            case Calendar.MONDAY:
                return "周一";
            case Calendar.TUESDAY:
                return "周二";
            case Calendar.WEDNESDAY:
                return "周三";
            case Calendar.THURSDAY:
                return "周四";
            case Calendar.FRIDAY:
                return "周五";
            case Calendar.SATURDAY:
                return "周六";
        }
        return "";
    }
}
